﻿using System;
using System.Collections.ObjectModel;
using System.Windows.Threading;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GrillMaster.Model;
using System.Windows.Input;
using GrillMaster.ServiceReference1;
using Microsoft.Phone.Scheduler;

namespace GrillMaster.ViewModel
{
    public class MainViewModel : ViewModelBase
    {
        public ObservableCollection<Animal> AnimalList { set; get; }    
        private bool alarmOn;
        private DispatcherTimer timer;
        private Alarm alarm;
        public MainViewModel()
        {
            StartStopButtonContent = "Start";
            alarmOn = false;
            alarm = new Alarm("MeatAlarm");
            var animals = new ObservableCollection<Animal>
                               {     
                                   new Animal("Beef Rare", 49),
                                   new Animal("Beef Medium", 60),
                                   new Animal("Beef Well Done", 72),
                                   new Animal("Chicke", 74),
                                   new Animal("Duck", 74),
                                   new Animal("Fish Steak", 60),
                                   new Animal("Human", 40),
                                   new Animal("Lamb Rare", 49),
                                   new Animal("Lamb Medium", 60),
                                   new Animal("Lamb Well Done", 72),
                                   new Animal("Pork", 71),
                                   new Animal("Turkey", 74),
                                   new Animal("Tuna", 52),
                               };
            SelectedAnimal = new Animal(animals[0].Name, animals[0].Temperature);
            SelectedName = animals[0].Name;
            SelectedTemperature = animals[0].Temperature;
            AnimalList = animals;

            ToggleStartStop = new RelayCommand(ToggleStartStopExecute);

            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 0, 2);
            timer.Tick += timer_Tick;
        }

        void timer_Tick(object sender, EventArgs e)
        {
            RetrievActualTemperature();
        }

        public void RetrievActualTemperature()
        {
            var client = new Service1Client();
            client.GetTemperatureAsync();
            client.GetTemperatureCompleted += (ClientGetTemperatureCompleted);
        }

        void ClientGetTemperatureCompleted(object sender, GetTemperatureCompletedEventArgs e)
        {
            CurrentTemperature = e.Result;
            CheckTemperature(CurrentTemperature);
        }

        void CheckTemperature(int temperature)
        {
            if(temperature >= SelectedTemperature)
            {
                alarm.BeginTime = DateTime.Now.AddSeconds(1);
                alarm.Content = "Meat Ready!";
                alarm.RecurrenceType = RecurrenceInterval.None;
                alarm.Sound = new Uri("/Sounds/Alarm.wma",UriKind.Relative);
                if(ScheduledActionService.Find(alarm.Name) as Alarm != null)
                {
                    ScheduledActionService.Remove(alarm.Name);
                }
                ScheduledActionService.Add(alarm);
                ToggleStartStopExecute();
            }   
        }

        public ICommand ToggleStartStop { set; get; }

        private void ToggleStartStopExecute()
        {
            if (!alarmOn)
            {
                StartStopButtonContent = "Stop";
                timer.Start();
                RetrievActualTemperature();
                alarmOn = true;            
            }
            else
            {
                StartStopButtonContent = "Start";
                timer.Stop();
                alarmOn = false;                   
            }

        }

        private string _startStopButtonContent;
        public string StartStopButtonContent
        {
            set { _startStopButtonContent = value; RaisePropertyChanged("StartStopButtonContent"); }
            get { return _startStopButtonContent; }
        }

        public const string MyPropertyPropertyName = "MyProperty";

        private int _currentTemperature;
        public int CurrentTemperature
        {
            set { _currentTemperature = value; RaisePropertyChanged("CurrentTemperature"); }
            get { return _currentTemperature; }
        }

        private string _selectedName;
        public string SelectedName
        {
            set { _selectedName = value; RaisePropertyChanged("SelectedName"); } 
            get { return _selectedName; }
        }

        private int _selectedTemperature;
        public int SelectedTemperature
        {
            set{ _selectedTemperature = value; RaisePropertyChanged("SelectedTemperature"); }
            get { return _selectedTemperature; }
        }

        private Animal _selectedAnimal;
        public Animal SelectedAnimal
        {
            set
            {
                if (SelectedAnimal == value)
                    return;
                _selectedAnimal = value;
                SelectedName = value.Name;
                SelectedTemperature = value.Temperature;
                RaisePropertyChanged("SelectedAnimal");
            } 
            get { return _selectedAnimal; }
        }
    }
}